
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    ctor(){
        this.nowPokerId = 0
        //操作掩码
        this.UG_HUA_MASK = 0xF0  // 1111 0000
        this.UG_VAL_MASK = 0x0F  // 0000 1111

        // cc.log(">>>>>>>>>>>>>>>>清空users")
        this.users = []
    },

    clearData:function () {
        cc.logfl("clear game data")
        // cc.logclient(">>>>>>>>>>>>>>>>清空users")
        this.room = null
        this.users = []
    },

    start () {

    },

    // update (dt) {},

    initTableMgr:function (tableInfo) {

        this.tableInfo = tableInfo

        this.pokerArray = this._initPokerPack()
        // this._shuffle()

        //dispatch
        for(let i=0;i<this.tableInfo.seats.length;i++){
            let seat = this.tableInfo.seats[i]
            seat.seatPlayer.pokers = this.getRandomArrayElements(this.pokerArray,13)
            cc.log("洗出来的牌：",seat.seatPlayer.pokers)
        }
    },

    getMixCard:function () {
        if(this.playGuild){
            return this.guildData.joker
        }
        return this.room.joker_obj
    },

    getMixCardNum:function () {
        return this.getPokerNum(this.getMixCard().poker)
    },

    getPlayerInfo:function () {
        return this.playerInfo
    },

    /**
     *
     * @returns player user entity in game
     */
    getPlayerUser:function () {
        // return this.users.get(this.playerId)
        for(let i in this.users){
            let user = this.users[i]
            if (user.uid === this.playerId)
                return user
        }
        return null
    },

    getUser:function (id) {
        for(let i in this.users){
            let user = this.users[i]
            if (user.id === id)
                return user
        }
        return null
    },

    clearAllUserTotalScore:function (baseScore) {
        for(let i in this.users){
            let user = this.users[i]
            user.score = baseScore
        }
        return null
    },

    getUserById:function (id) {
        for(let i in this.users){
            let user = this.users[i]
            if (user.id === id)
                return user
        }
        return null
    },

    getPlayerHandCardObjs:function () {
        if(this.playGuild){
            return this.guildData.cards
        }
        return this.getPlayerUser().cardObjs
    },

    /**
     * player info in global
     * @param info
     */
    setPlayerInfo:function (info) {
        // cc.logfl("save player user info")
        this.playerId = info ? info.uid : null
        this.playerInfo = info
    },

    getPlayerId:function () {
        return this.playerId
    },

    getPlayerGameId:function () {
        if(this.playGuild){
            return this.playerId
        }
        return this.getPlayerUser().id
    },

    convertToClientPokerObj:function (cardArray) {
        let objs = []
        for(let i=0;i<cardArray.length;i++){
            let num = this.getPokerNum(cardArray[i])
            if(num === 14 && !this.isJoker(cardArray[i])){
                cardArray[i] = cardArray[i] >> 4
                cardArray[i] = cardArray[i] << 4
                cardArray[i] += 1
            }
            objs.push({id:this.generateNewPokerId(),poker:cardArray[i]})
        }

        return objs
    },

    convToClientPokerObj:function (cardValue) {
        let v = this.convToClientPokerValue(cardValue)
        return {id:this.generateNewPokerId(),poker:v}
    },

    convToClientPokerValue:function (cardValue) {
        let num = this.getPokerNum(cardValue)
        if(num === 14 && !this.isJoker(cardValue)){
            cardValue = cardValue >> 4
            cardValue = cardValue << 4
            cardValue += 1
        }
        return cardValue
    },

    convToClientPokerValueArray:function (serCardValueArray) {
        let serverArr = []
        for(let i=0;i<serCardValueArray.length;i++){
            let tmpArr = []
            for(let j=0;j<serCardValueArray[i].length;j++){
                tmpArr.push(this.convToClientPokerValue(serCardValueArray[i][j]))
            }
            serverArr.push(tmpArr)
        }
        return serverArr
    },

    convToServerPokerValue:function (cardValue) {
        let num = this.getPokerNum(cardValue)
        if(num === 1 && !this.isJoker(cardValue)){
            cardValue = cardValue >> 4
            cardValue = cardValue << 4
            cardValue += 14
        }
        return cardValue
    },

    convToServerPokervalueArray:function (cardValueArray) {
        let serverArr = []
        for(let i=0;i<cardValueArray.length;i++){
            serverArr.push(this.convToServerPokerValue(cardValueArray[i]))
        }
        return serverArr
    },

    _initPokerPack:function () {

        let pokerArray = []
        let array = [
            0x01,0x11,0x21,0x31,  //方块2~黑桃2 1,17,33,49,65,66
            0x02,0x12,0x22,0x32,  //方块3~黑桃3
            0x03,0x13,0x23,0x33,  //方块4~黑桃4
            0x04,0x14,0x24,0x34,  //方块5~黑桃5
            0x05,0x15,0x25,0x35,  //方块6~黑桃6
            0x06,0x16,0x26,0x36,  //方块7~黑桃7
            0x07,0x17,0x27,0x37,  //方块8~黑桃8
            0x08,0x18,0x28,0x38,  //方块9~黑桃9
            0x09,0x19,0x29,0x39,  //方块10~黑桃10
            0x0A,0x1A,0x2A,0x3A,  //方块J~黑桃J
            0x0B,0x1B,0x2B,0x3B,  //方块Q~黑桃Q
            0x0C,0x1C,0x2C,0x3C,  //方块K~黑桃K
            0x0D,0x1D,0x2D,0x3D,  //方块A~黑桃A
            0x4E,0x4F,
        ]
        cc.log("all array:",array)

        pokerArray = pokerArray.concat(array)

        let pokerObj = []
        for(let i=1;i<pokerArray.length;i++){
            pokerObj.push({id:this.generateNewPokerId(),poker:pokerArray[i]})
        }
        return pokerObj
    },

    getPokerName:function (pokerId) {
        if(!pokerId)
            return "没有牌"

        let hua = this.getPokerHua(pokerId)
        let huaString
        if(hua === cc.gg.enumConfig.FLOWER.BLACK){
            huaString = "黑桃"
        }else if(hua === cc.gg.enumConfig.FLOWER.HEART){
            huaString = "红桃"
        }else if(hua === cc.gg.enumConfig.FLOWER.FLOWER){
            huaString = "梅花"
        }else if(hua === cc.gg.enumConfig.FLOWER.DIAMOND){
            huaString = "方块"
        }else if(hua === cc.gg.enumConfig.FLOWER.JOKER){
            huaString = "王"
        }else {
            huaString = "未知"
        }

        let num = this.getPokerNum(pokerId)
        return huaString + num
    },

    generateNewPokerId:function () {
        this.nowPokerId ++
        return this.nowPokerId
    },

    generateNewPoker:function (pokerNum) {
        return {
            poker:pokerNum,
            id:this.generateNewPokerId()
        }
    },

    _shuffle:function () {
        if(!this.pokerArray){
            cc.error("代码错误")
            return
        }

        function randomsort(a, b) {
            return Math.random()>.5 ? -1 : 1;
            //用Math.random()函数生成0~1之间的随机数与0.5比较，返回-1或1
        }

        this.pokerArray.sort(randomsort);
    },

    getRandomArrayElements:function (arr, count) {
        var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
        while (i-- > min) {
            index = Math.floor((i + 1) * Math.random());
            temp = shuffled[index];
            shuffled[index] = shuffled[i];
            shuffled[i] = temp;
        }
        return shuffled.slice(min);
    },

    //获取花色
    getPokerHua:function(iPoker)
    {
        let hua = iPoker >> 4
        hua+=1
        return hua
    },

    //获取牌值(满足牌值定义做一个偏移)
    getPokerNum:function (iPoker)
    {
        if(!iPoker) return 0
        return iPoker & this.UG_VAL_MASK
    },

    getPokerScore:function (iPoker) {
        let num = this.getPokerNum(iPoker)
        if(num === 1) return 10
        else return Math.min(num,10)
    },

    isJokerPoint:function (num) {
        return num === 14 || num === 15
    },

    isJokerFlower:function (tarHua) {
        return tarHua === cc.gg.enumConfig.FLOWER.JOKER
    },

    isJoker:function (pokerId) {
        return pokerId === 0x4E || pokerId === 0x4F
    },

    isMixCard:function (cardId,mixNum) {
        let _tar_num = this.getPokerNum(cardId)
        if(this.isJokerPoint(_tar_num)) return true

        mixNum = mixNum || this.getPokerNum(this.getMixCard().poker)
        if(!mixNum) return false

        return _tar_num === mixNum
    },

    getCardId:function (flower, num) {
        if(flower === cc.gg.enumConfig.FLOWER.BLACK){
            return 0 + num
        }else if(flower === cc.gg.enumConfig.FLOWER.HEART){
            return 16 + num
        }else if(flower === cc.gg.enumConfig.FLOWER.FLOWER){
            return 32 + num
        }else if(flower === cc.gg.enumConfig.FLOWER.DIAMOND){
            return 48 + num
        }else if(flower === cc.gg.enumConfig.FLOWER.JOKER){
            return 64 + num
        }else {
            cc.error("unknown flower:",flower)
            return 0 + num
        }
    }
});
